'use strict'

import httpUtil from './HttpUtil'

import store from '../../store/store'

const param = {
  isLogin: '/stu2/isLogin',
  getSystemParams: '/stu2/system/getSystemParams'
}

/**
 * 验证是否登录
 * @returns {Promise<*>}
 */
export async function isLogin () {
  return new Promise(resolve => {
    httpUtil.xhrGet(window.selfVue, param.isLogin, {}, data => {
      const flag = data.code === 0
      if (flag) {
        store.commit('changeLoginState', 1)
        store.commit('saveLoginInfo', data.data)
      }
      resolve({ flag, info: data.data })
    })
  })
}

/**
 * 获取基本参数
 * @returns {Promise<*>}
 */
export async function getSystemParams () {
  return new Promise(resolve => {
    httpUtil.xhrGet(window.selfVue, param.getSystemParams, {}, data => {
      store.commit('updateParams', data)
      resolve(data.data)
    }, httpUtil.getParam().ResponseType.JSON, {mask: false})
  })
}
